package com.mag.servlet;

import com.mag.domain.Suppliers;
import com.mag.service.SuppliersService;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class Addsu extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {

        response.setContentType("text/html");
        SuppliersService Ss = new SuppliersService();
        String SupplierID = request.getParameter("SupplierID");
        String SupplierName = request.getParameter("SupplierName");
        String SupplierAddress = request.getParameter("SupplierAddress");
        String WebSite = request.getParameter("WebSite");
        String SupplierPhone = request.getParameter("SupplierPhone");
        String FaxSupplier = request.getParameter("FaxSupplier");
        String OfficialEmail = request.getParameter("OfficialEmail");
        String PersonContact = request.getParameter("PersonContact");
        String PhoneContact = request.getParameter("PhoneContact");
        String DateBulid = request.getParameter("DateBulid");
        String GoodsSupply = request.getParameter("GoodsSupply");
        String DateFirstSupply = request.getParameter("DateFirstSupply");
        int AccumulativeTime = Integer.parseInt(request.getParameter("AccumulativeTime"));
        int AccumulativeLevel = Integer.parseInt(request.getParameter("AccumulativeLevel"));
        int NumFault = Integer.parseInt(request.getParameter("NumFault"));
        int NumBacklogging = Integer.parseInt(request.getParameter("NumBacklogging"));
        double TotalMoney = Double.parseDouble(request.getParameter("TotalMoney"));
        double NoFaultRate = Double.parseDouble(request.getParameter("NoFaultRate"));
        double DeliveryOnTime = Double.parseDouble(request.getParameter("DeliveryOnTime"));

        Suppliers supplier = new Suppliers();
        supplier.setSupplierID(SupplierID);
        supplier.setSupplierName(SupplierName);
        supplier.setSupplierAddress(SupplierAddress);
        supplier.setWebSite(WebSite);
        supplier.setSupplierPhone(SupplierPhone);
        supplier.setFaxSupplier(FaxSupplier);
        supplier.setOfficialEmail(OfficialEmail);
        supplier.setPersonContact(PersonContact);
        supplier.setPhoneContact(PhoneContact);
        supplier.setDateBulid(DateBulid);
        supplier.setGoodsSupply(GoodsSupply);
        supplier.setDateFirstSupply(DateFirstSupply);
        supplier.setAccumulativeTime(AccumulativeTime);
        supplier.setAccumulativeLevel(AccumulativeLevel);
        supplier.setNumFault(NumFault);
        supplier.setNumBacklogging(NumBacklogging);
        supplier.setTotalMoney(TotalMoney);
        supplier.setNoFaultRate(NoFaultRate);
        supplier.setDeliveryOnTime(DeliveryOnTime);

        int count = Ss.GetCountOfSupplierByID(SupplierID);
        if (count == 0) {
            Ss.AddSupplier(supplier);

            Suppliers supplier0 = Ss.CheckSupplier(supplier);
            request.getSession().setAttribute("updateSupplier", supplier0);
            response.sendRedirect("/manager/jsp/update.jsp");
        } else {
            response.setCharacterEncoding("UTF-8");
            response.setContentType("text/html;charset=UTF-8");
            PrintWriter out = response.getWriter();
            out.println("<script>alert('\\u4F9B\\u5E94\\u5546ID\\u5DF2\\u5B58\\u5728\\uFF01');</script>");
            out.println("<script>window.location.href='/manager/jsp/AddSupplier.jsp';</script>");
            out.flush();
        }
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");

        response.getWriter();

        this.doGet(request, response);
    }

}